package com.mc.book.utils.sse;

import lombok.experimental.UtilityClass;
import okhttp3.*;

import java.util.concurrent.TimeUnit;

/**
 * TODO
 *
 * @Author: dong
 * @Date: 2024/7/2 14:04
 */
@UtilityClass
public class SSEHttp {
    private static OkHttpClient okHttpClient;

    public static ConnectionPool connectionPool = new ConnectionPool(10, 5, TimeUnit.MINUTES);
    public static OkHttpClient getInstance() {
        if (okHttpClient == null) { //加同步安全
            synchronized (OkHttpClient.class) {
                if (okHttpClient == null) { //okhttp可以缓存数据....指定缓存路径
                    //构建器
                    okHttpClient = new OkHttpClient
                            .Builder()
                            //来屏蔽系统代理
//                            .proxy(Proxy.NO_PROXY)
//                            .connectionPool(connectionPool)
                            .connectTimeout(10, TimeUnit.SECONDS)//连接超时
                            .writeTimeout(50, TimeUnit.SECONDS)//写入超时
                            .readTimeout(10, TimeUnit.SECONDS)//读取超时
                            .build();
                    okHttpClient.dispatcher().setMaxRequestsPerHost(200);
                    okHttpClient.dispatcher().setMaxRequests(200);
                }
            }
        }
        return okHttpClient;
    }


}
